package com.king.login.controller;

import com.king.platform.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageInfo;
import com.king.login.entity.Member;
import com.king.login.service.MemberService;
import com.king.login.param.MemberParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import com.king.platform.enmu.ResultEnum;
import com.king.platform.entity.BaseResult;

import java.util.List;

/**
 * <p>todo Member 表控制器</p>
 * @author: cuizx
 * @created: 2024-03-29 20:28:39
 * @version v1.0.1
 * @copyright: Copyright (c)  cuizx
 */

@Api(tags = "member-api")
@RestController
@RequestMapping("/member")
public class MemberController
{

  @Autowired
  private MemberService memberService;

  /**
   * <p>todo 查询所有</p>
   * @param: model
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @PostMapping("/list")
  @ApiOperation(value = "查询所有", notes = "查询所有")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "mode", value = "查询参数", dataTypeClass = String.class)
  })
  public BaseResult<List<Member>> list(@RequestBody MemberParam model)
  {

    //执行查询
    List<Member> list = memberService.list(model);

    //封装查询结果
    return BaseResult.success(ResultEnum.SUCCESS_CODE, list);
  }

  /**
   * <p>todo 分页查询</p>
   * @param: model
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @PostMapping("/listPage")
  @ApiOperation(value = "分页查询", notes = "分页查询")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "mode", value = "查询参数", dataTypeClass = String.class)
  })
  public BaseResult<PageInfo<Member>> listPage(@RequestBody MemberParam model)
  {

    //执行查询
    PageInfo<Member> pageInfo = memberService.listPage(model);

    //封装查询结果
    return BaseResult.success(ResultEnum.SUCCESS_CODE, pageInfo);
  }

  /**
   * <p>todo 保存</p>
   * @param: model
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @PostMapping("/insert")
  @ApiOperation(value = "保存", notes = "保存")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "mode", value = "存储对象", dataTypeClass = String.class)
  })
  public BaseResult insert(@RequestBody Member model)
  {

    memberService.save(model);
    //封装查询结果
    return model.getId() > 0 ? BaseResult.success(ResultEnum.SUCCESS_CODE) : BaseResult.failed(ResultEnum.FAILED_CODE);
  }

  /**
   * <p>todo 根据id更新</p>
   * @param: model
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @PutMapping("/updateById")
  @ApiOperation(value = "更新", notes = "更新")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "mode", value = "更新对象", dataTypeClass = String.class)
  })
  public BaseResult updateById(@RequestBody Member model)
  {

    if (model.getId() == null)
    {
      throw new BizException(900001, "用户id不存在");
    }
    memberService.updateById(model);
    //封装查询结果
    return BaseResult.success(ResultEnum.SUCCESS_CODE);
  }


  /**
   * <p>todo 根据id查询</p>
   * @param: id
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @GetMapping(value = "/getById/{id}")
  @ApiOperation(value = "根据id查询", notes = "根据id查询")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "ID", dataTypeClass = Long.class)
  })
  public BaseResult<Member> getById(@PathVariable("id") Long id)
  {
    Member temp = memberService.getById(id);
    //封装查询结果
    return BaseResult.success(ResultEnum.SUCCESS_CODE, temp);
  }

  /**
   * <p>todo 根据id删除</p>
   * @param: id
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @DeleteMapping("/deleteById/{id}")
  @ApiOperation(value = "根据id删除", notes = "根据id删除")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "ID", dataTypeClass = Long.class)
  })
  public BaseResult deleteById(@PathVariable("id") Long id)
  {
    memberService.removeById(id);
    //封装查询结果
    return BaseResult.success(ResultEnum.SUCCESS_CODE);
  }

  /**
   * <p>todo 批量删除</p>
   * @param: ids
   * @return: Member
   * @author: cuizx
   * @date: 2024-03-29 20:28:39
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2024-03-29 20:28:39    cuizx        v1.0.1             init
   */
  @DeleteMapping("/deleteByIds")
  @ApiOperation(value = "批量删除", notes = "批量删除")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "ids", value = "用户ID字符串,逗号拼接", dataTypeClass = String.class)
  })
  public BaseResult deleteByIds(@RequestParam("ids") String ids)
  {
    if (ids == null)
    {
      throw new BizException(900001, "用户id不存在");
    }
    memberService.deleteByIds(ids);
    //封装查询结果
    return BaseResult.success(ResultEnum.SUCCESS_CODE);
  }


}


